﻿using System;
using System.Collections.Generic;

namespace SoftSource.Common
{
    /// <summary>
    /// These extensions are intended for a queue that is used to store actions that need to be executed in order.
    /// </summary>
    public static class QueueExtensions
    {
        public static void ExecuteAll<T>(this Queue<T> queue, Action<T> executor)
        {
            queue.ExecuteAll(executor, null);
        }

        public static void ExecuteAll<T>(this Queue<T> queue, Action<T> executor, Action completion)
        {
            if (queue == null)
                return;

            int count = queue.Count;

            if (queue != null && count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    T value = queue.Dequeue();
                    executor(value);
                }
            }

            if (completion != null)
                completion();
        }

        public static bool ExecuteNext<T>(this Queue<T> queue, Action<T> executor)
        {
            if (queue != null && queue.Count > 0)
            {
                T value = queue.Dequeue();
                executor(value);
                return true;
            }

            return false;
        }

        public static void ExecuteAll(this Queue<Action> queue)
        {
            queue.ExecuteAll(action => action());
        }

        public static void ExecuteAll(this Queue<Action> queue, Action completion)
        {
            queue.ExecuteAll(action => action(), completion);
        }

        public static bool ExecuteNext(this Queue<Action> queue)
        {
            return queue.ExecuteNext(action => action());
        }
    }
}
